import {reqGoodInfo, reqAddOrUpdateShopoCart} from "@/api"
import {getUUID} from '@/utils/uuid_token'

const state = {
    goodInfo: {},
    // 游客临时身份
    uuid_token: getUUID()
}
const mutations = {
    GETGOODINFO(state, goodInfo) {
        state.goodInfo = goodInfo
    }
}
const actions = {
    async getGoodDetail({commit}, skuId) {
        let result = await reqGoodInfo(skuId);
        if (result.code == 200) {
            commit('GETGOODINFO', result.data)
        }
    },
    //将商品添加到购物车中
    // eslint-disable-next-line no-unused-vars
    async addOrUpdateShopoCart({commit}, {skuId, skuNum}) {
      let result = await reqAddOrUpdateShopoCart(skuId, skuNum);
      // 当前函数执行返回Promise 只需要给出加入购物车成功或失败
      if (result.code == 200) {
        return "加入购物车成功!"
      } else {
        return Promise.reject(new Error("加入购物车失败,请稍后再试!"))
      }
    }
}
const getters = {
    categoryView(state) {
        return state.goodInfo.categoryView || {}
    },
    skuInfo(state) {
        return state.goodInfo.skuInfo || {}
    },
    spuSaleAttrList(state) {
        return state.goodInfo.spuSaleAttrList || {}
    }
}

export default ({
    state,
    mutations,
    actions,
    getters
})